package com.bibi.hanshunping.chapter07;

public class MiGong {
    public static void main(String[] args) {
        // 1. 创建一个二维数组，表示迷宫
        // 2. 0表示可以做，1表示障碍物
        int[][] map = new int[8][7];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }

        // 初始迷宫
        for (int i = 0; i < map.length; i++) {
            map[i][0] = 1;
            map[i][map[i].length - 1] = 1;
        }

        for (int i = 0; i < map[0].length; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }

        map[4][1] = 1;
        map[4][2] = 1;
        System.out.println("=====================");
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }

        MiGong miGong = new MiGong();
        miGong.step(map, 1, 1);
        System.out.println("==========结果===========");
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 下右上左 的顺序找路线
     */
    public boolean step(int[][] map, int i, int j) {

        if (map[6][5] == 2) {
            return true;
        }
        if (map[i][j] == 0) {
            // 假定可以走通
            map[i][j] = 2;
            if (step(map, i + 1, j)) {
                return true;
            } else if (step(map, i, j + 1)) {
                return true;
            } else if (step(map, i - 1, j)) {
                return true;
            } else if (step(map, i, j - 1)) {
                return true;
            } else {
                map[i][j] = 3;
                return false;
            }
        }
        return false;
    }
}
